package xyz.xiaoshengfu.xrpc.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import xyz.xiaoshengfu.xrpc.admin.service.XRpcAdminService;

/**
 * <p>Title: XRpcAdminController</p>
 * <p>Description: xrpc-admin Controller</p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年3月25日
 */
@Controller
public class XRpcAdminController {

	@Autowired
	private XRpcAdminService xRpcAdminService;

	@RequestMapping("/")
	public String index(String applicationName, HttpServletRequest request) {
		if (StringUtils.isNoneBlank(applicationName)) {
			request.setAttribute("xRpcConfigList",
					xRpcAdminService.getXRpcConfigListByApplicationName(applicationName));
			request.setAttribute("applicationName", applicationName);
		} else {
			request.setAttribute("xRpcConfigList", xRpcAdminService.getXRpcConfigList());
		}
		request.setAttribute("xRpcConfigListInfor", xRpcAdminService.getXRpcConfigListInfor());
		return "index";
	}

	@RequestMapping("/xrpc-admin/login")
	public String adminLogin(String account, String password, HttpServletRequest request) {
		boolean login = xRpcAdminService.adminLogin(account, password);
		if (login) {
			request.getSession().setAttribute("adminLogin", login);
			return "redirect:/";
		}
		return "login";
	}

	@RequestMapping("/xrpc-admin/login_page")
	public String showAdminLoginPage() {
		return "login";
	}

	@RequestMapping("/xrpc-admin/application/{applicationName}")
	public String showApplicationInfo(@PathVariable String applicationName, HttpServletRequest request) {
		if (StringUtils.isNoneBlank(applicationName)) {
			request.setAttribute("xRpcConfig", xRpcAdminService.getXRpcConfigByApplicationName(applicationName));
			request.setAttribute("applicationReferenceServiceListMap",
					xRpcAdminService.getApplicationReferenceServiceListMap(applicationName));
			request.setAttribute("applicationServiceReferenceListMap",
					xRpcAdminService.getApplicationServiceReferenceListMap(applicationName));
		}
		return "application";
	}

}